﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Text.RegularExpressions;
namespace atmsystem_ss
{


    public partial class frmCreate_Customer : Form
    {
        private SqlConnection con = new SqlConnection();
        private DataTable dt = new DataTable("tblCustomer");
        private SqlDataAdapter da = new SqlDataAdapter();
        private Boolean kt;

        private void connect()
        {
            string cn = "Server=.\\SQLEXPRESS;Database=atmsystemsangson;User Id=sa;Password=123456";
            try
            {
                con = new SqlConnection(cn);
                con.Open();//mo ket noi

            }
            catch (Exception ex)
            {
                MessageBox.Show("Khong ket noi duoc co so du lieu", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        private void disconnect()
        {
            con.Close();//dong ket noi
            con.Dispose();//giai phong tai nguyen
            con = null;//huy doi tuong

        }
        private void getdata()
        {
            SqlCommand command = new SqlCommand();// khai bao 1 command
            command.Connection = con; //ket noi
            command.CommandType = CommandType.Text;//khai bao kieu command
            command.CommandText = @"Select * from tblCustomer";
            da.SelectCommand = command;
            da.Fill(dt);
            }
        private bool isValidEmail(string inputEmail)
        {
            string strRegex = @"^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}" +
                  @"\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\" +
                  @".)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$";
            Regex re = new Regex(strRegex);
            if (re.IsMatch(inputEmail))
                return (true);
            else
                return (false);
        }
        private bool IsDateTime(string txtDate)
        {
            DateTime tempDate;
            return DateTime.TryParse(txtDate, out tempDate) ? true : false;
        }
        public frmCreate_Customer()
        {
            InitializeComponent();
        }
        
        private void frmCreate_Customer_Load(object sender, EventArgs e)
        {
            connect();
            getdata();
        }
        private bool tontai()
        {
            dt.Clear();
            SqlCommand command = new SqlCommand();
            command.Connection = con;
            command.CommandType = CommandType.Text;
            command.CommandText = @"Select * from tblCustomer where fldID_Customer LIKE '%'+@fldID_Customer+'%' ";
            command.Parameters.Add("@fldID_Customer", SqlDbType.NVarChar, 50).Value = txtID_Customer.Text;
            da.SelectCommand = command;
            da.Fill(dt);
            if (dt.Rows.Count > 0)
            {
                return false;
            }
            return true;

        }
        private void btnCancel_Click(object sender, EventArgs e)
        {
          
            Close();
            Dispose();
            disconnect();
            frmInformation_Customer _frmInformation_Customer = new frmInformation_Customer();
            _frmInformation_Customer.Show();
        }
        private void ClearTextBox()
        {
            txtAddress.Text = "";
            txtBirth.Text = "";
            txtEmail.Text = "";
            txtFull_Name.Text = "";
            txtID_Customer.Text = "";
            txtID_Number.Text = "";
            txtPhone.Text = "";
            cboSex.Text = "";

        }

        private bool kiemtra() {
            kt = true;
            if (txtID_Customer.Text == "")
            {
                txtID_Customer.Focus();
                MessageBox.Show("Ban chua nhap Customer ID!", " Thong Bao", MessageBoxButtons.OK);
                kt = false;

            }
            else if (tontai() == false)
            {
                txtID_Customer.Text = "";
                txtID_Customer.Focus();
                MessageBox.Show("Customer ID da ton tai, ban vui long nhap Customer ID khac !", "Thong bao", MessageBoxButtons.OK);
                kt = false;
            }
            else if (txtFull_Name.Text == "")
            {
                txtFull_Name.Focus();
                MessageBox.Show("Ban chua nhap Full Name!", " Thong Bao", MessageBoxButtons.OK);
                kt = false;

            }
            else if (txtID_Number.Text == "")
            {
                txtID_Number.Focus();
                MessageBox.Show("Ban chua nhap ID Number!", " Thong Bao", MessageBoxButtons.OK);
                kt = false;

            }
            else if (cboSex.Text == "")
            {
                cboSex.Focus();
                MessageBox.Show("Ban chua chon Sex!", " Thong Bao", MessageBoxButtons.OK);
                kt = false;

            }
            else if (txtBirth.Text == "")
            {
                txtBirth.Focus();
                MessageBox.Show("Ban chua nhap Birth!", " Thong Bao", MessageBoxButtons.OK);
                kt = false;

            }
            else if (IsDateTime(txtBirth.Text.ToString()) == false)
            {
                txtBirth.Focus();
                MessageBox.Show("The format invalid!", " Error", MessageBoxButtons.OK);
                kt = false;
            }
            else if (txtAddress.Text == "")
            {
                txtAddress.Focus();
                MessageBox.Show("Ban chua nhap Address!", " Thong Bao", MessageBoxButtons.OK);
                kt = false;

            }
            else if (txtPhone.Text == "")
            {
                txtPhone.Focus();
                MessageBox.Show("Ban chua nhap Phone!", " Thong Bao", MessageBoxButtons.OK);
                kt = false;

            }
            else if (txtEmail.Text == "")
            {
                txtEmail.Focus();
                MessageBox.Show("Ban chua nhap Email!", " Thong Bao", MessageBoxButtons.OK);
                kt = false;

            }
            else if (isValidEmail(txtEmail.Text.ToString()) == false)
            {
                txtEmail.Focus();
                MessageBox.Show("The format invalid!", " Error", MessageBoxButtons.OK);
                kt = false;
            }
            return kt;
        }
        private void btnSave_Click(object sender, EventArgs e)
        {
            kiemtra();
            if (kt == true)
            {
                DataRow row = dt.NewRow();
                row["fldID_Customer"] = txtID_Customer.Text;
                row["fldFull_Name"] = txtFull_Name.Text;
                row["fldID_Number"] = txtID_Number.Text;
                if (cboSex.Text == "Male")
                    row["fldSex"] = true;
                else if (cboSex.Text == "Female")
                    row["fldSex"] = false;
                row["fldBirth"] = txtBirth.Text;
                row["fldAddress"] = txtAddress.Text;
                row["fldPhone"] = txtPhone.Text;
                row["fldEmail"] = txtEmail.Text;
                dt.Rows.Add(row);
                SqlCommand commandInsert = new SqlCommand();
                commandInsert.Connection = con;
                commandInsert.CommandType = CommandType.Text;
                commandInsert.CommandText = @"Insert tblCustomer (fldID_Customer, fldFull_Name, fldID_Number, fldSex, fldBirth, fldAddress, fldPhone, fldEmail) Values (@fldID_Customer, @fldFull_Name, @fldID_Number, @fldSex, @fldBirth, @fldAddress, @fldPhone, @fldEmail)";
                commandInsert.Parameters.Add("@fldID_Customer", SqlDbType.NVarChar, 50, "fldID_Customer");
                commandInsert.Parameters.Add("@fldFull_Name", SqlDbType.NVarChar, 50, "fldFull_Name");
                commandInsert.Parameters.Add("@fldID_Number", SqlDbType.NVarChar, 50, "fldID_Number");
                commandInsert.Parameters.Add("@fldSex", SqlDbType.Bit, 50, "fldSex");
                commandInsert.Parameters.Add("@fldBirth", SqlDbType.DateTime, 50, "fldBirth");
                commandInsert.Parameters.Add("@fldAddress", SqlDbType.NVarChar, 50, "fldAddress");
                commandInsert.Parameters.Add("@fldPhone", SqlDbType.NVarChar, 50, "fldPhone");
                commandInsert.Parameters.Add("@fldEmail", SqlDbType.NVarChar, 50, "fldEmail");
                da.InsertCommand = commandInsert;
                da.Update(dt);
                MessageBox.Show("Ban da insert thanh cong", "Insert successed", MessageBoxButtons.OK, MessageBoxIcon.Information);
                ClearTextBox();
            }
        }
    }
}
